package k8s

import (
	"context"
	"fmt"
	"k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"log"
)

var ClientSet *kubernetes.Clientset

var DynamicClient *dynamic.DynamicClient

func Initk8s() {
	//赋值变量
	var kubeconfigContent = `
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURCVENDQWUyZ0F3SUJBZ0lJT0NKSmlHc0ljTTR3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TlRBeE1qQXdOelU0TVRWYUZ3MHpOVEF4TVRnd09EQXpNVFZhTUJVeApFekFSQmdOVkJBTVRDbXQxWW1WeWJtVjBaWE13Z2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBQTRJQkR3QXdnZ0VLCkFvSUJBUURpR1dmVXFFUzZMV1NMSW1sVUl5SlJKYVRmSHlacy92am1pT2dhc21KSTM4OTZzWkxndklFb2JHTm0KbHpBVHUvUFUrQmhHM1U5OEZ4SHpnc0ZwZUdxeS9ZTTd5c0U5UjQ2MEhTcS9BeTRvalZESldTRkZ4WUFFSWtncgpkNUd5bkY5YXVEOEdCMnRLRTdPVG0vdVBld3NIZSs3T0grcm4xc0NldWhycmZFRnIzQTFlRnFuOXMrd25XUmY2Cm9TbHQwT2Z6QUpXSm9uSGVCV29VOXZIaG5mYVV6cFdDT1pEQ2pNRFJxQzJpY1ErNFg3WVZ5ZkZQQlRmdll5dXUKOWwxN0dNL2RNYnUvcnk0SXFBSGlHU2NhS1FaWmIrRFM0MFhOQTNOaHhQVDl3SWUxbXc3T2FpUGxkVDlLbFp3bwo5NDk2OXpWbGxwQytVVC9HV2IrWmo1YlFTUDJMQWdNQkFBR2pXVEJYTUE0R0ExVWREd0VCL3dRRUF3SUNwREFQCkJnTlZIUk1CQWY4RUJUQURBUUgvTUIwR0ExVWREZ1FXQkJTNVpMR0xiVXRVSVYwTFBKSGVnOCtKT3JpTWhEQVYKQmdOVkhSRUVEakFNZ2dwcmRXSmxjbTVsZEdWek1BMEdDU3FHU0liM0RRRUJDd1VBQTRJQkFRQklHNXQ1VXQyNgpwckFNWHhSY2NvQTRUeDloeUJsTTRWL1ZWc2pqc1hNRU8rZndGNkVVRGxCSGE0UVdPNlpWYkdQNEg5czR4U3pUCmlyR3daZlZBcmo4M2VKM3RDdGdkQTlwd0RwWkFoem1tSTRrVXNsR3dKa085NzdiYXpGMFhjOTV5RXd0eUZueC8Ka0wraVp2UUlWUlhvNS9aRFdIMEVITGg4Qk9hcnh2OHl3NUt0bkk3NWxxVE1La21iWnZyNVhHbldJanpwZmxyZwp5d0x4b2RoTytvVHhPNlFCeGJGUWRCVEtGT1RueU1uQ25YNDJYKzBxRjRQNjNGdWZoZ05RK1pjZ3lIbDlFdHlsCmhOWkp3Qmc0NWJZUmJMMEdVMlJ6M3g1N3NuQWRFSzk1eDBnWjlRaVlwVFJUQzZkQU5qdC83QnRmMW5NTEJGRHAKTWxQVHJodS9BYjJiCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
    server: https://47.243.29.176:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURLVENDQWhHZ0F3SUJBZ0lJQWxTZURySHFIZFl3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TlRBeE1qQXdOelU0TVRWYUZ3MHlOakF4TWpBd09EQXpNVFZhTUR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V5L0hwSlpUbU1ybFFpeW9sZWFrbTA3eDZpTgpNbU94d2p5UU1QU3NwaG1UNDJLTHg3ZUxCb2Nyb1laanp6NHhNYjN4S21aZ0dsbEsyL0RBTit2TVZ1RGhTN2Y3Cm5kYXBBb0dBVGpvd0I4ZGtDZmUyVlEzU1VDaFpFRzg3STBaTWVRRVllcGlsYnV3Z0FDUkZoZVF6NnJLWDBST08KUmh2TENPUjNYMitvVGVvMXRKeWpUVHFMN0g4UVVrY2ZUdHhVZUdqUFA1YmZJbThDV2lQYmtWRW1mWlVDYnlPdApGQXUzV0pTNmRyNFpEWnBSNVVTZTljWm1KS2NndU1IMnZpMW1sV3FjcnVGYU9qNkRtM1k9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
`
	//加载配置
	config, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfigContent))

	if err != nil {
		log.Fatalf("Error loading kubeconfig: %s", err.Error())
		return
	}

	//实例化 ClientSet
	ClientSet, err = kubernetes.NewForConfig(config)
	if err != nil {
		panic(err)
	}

	// 2、实例化动态客户端对象
	DynamicClient, err = dynamic.NewForConfig(config)
	if err != nil {
		log.Fatalf("Error creating dynamic client: %s", err.Error())
	}

	// 3、配置需要调用的GVR
	/*	gvr := schema.GroupVersionResource{
		Group:    "cilium.io",   // API Group 设置为 cilium.io
		Version:  "v2",          // 版本设置为 v2
		Resource: "ciliumnodes", // 资源名称为 ciliumnodes
	}*/

	gvr := schema.GroupVersionResource{
		Group:    "kubevirt.io",
		Version:  "v1",
		Resource: "virtualmachineinstances",
	}

	// 4、发送请求，且得到返回结果(动态客户端获取到的是非结构化结构体，需要用非结构化变量去存储)
	unStructData, err := DynamicClient.Resource(gvr).Namespace("default").List(context.TODO(), v1.ListOptions{})
	if err != nil {
		log.Fatalf("Error fetching ciliumnodes: %s", err.Error())
	}

	// 5、打印返回的 unstructured 数据
	for _, item := range unStructData.Items {
		fmt.Println(item.GetNamespace(), item.GetName())
	}

	for _, pod := range unStructData.Items {
		name := pod.GetName()

		// 获取 Pod 的 status 字段
		status, found, err := unstructured.NestedString(pod.Object, "status", "ipAddress")
		if err != nil || !found {
			fmt.Printf("- %s: [Status not found]\n", name)
			continue
		}

		fmt.Printf("- %s: %s\n", name, status)
	}
}
